/*
 * Copyright Eduardo Aponte 2013
 * aponteeduardo@gmail.com
 */
#include <eyelink.h>
#include "mex.h"
#include "utils.h"


INT16 _main( UINT32 msec, INT16 samples, INT16 events )
{
    return eyelink_wait_for_data( msec, samples, events );
}

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
    mwSize td[2] = {1,1};
    double *status;
    
    int m,s,e;    
    
    /* Verify input arguments. */
    if(nrhs != 3 ) {
        mexErrMsgTxt("Incorrect number of inputs.");
    } else if( nlhs > 1 ) {
        mexErrMsgTxt("Too many output arguments.");
    }

    if ( isscalar( prhs[0] ) ){
        INT16 scalar;
        /* Returns the scalar, casts to int if necessary. Returns 0 if */
        /* input is complex. */
        if ( returnscalar( prhs[0], &m ) != 1 )
            mexErrMsgTxt("Complex data is not supported.");                
        if ( m < 0 )
            mexErrMsgTxt("mssec should be positive.");
    } else {
        mexErrMsgTxt("msec should be a scalar."); 
    }
    
    if ( isscalar( prhs[1] ) ){
        INT16 scalar;
        /* Returns the scalar, casts to int if necessary. Returns 0 if */
        /* input is complex. */
        if ( returnscalar( prhs[1], &s ) != 1 )
            mexErrMsgTxt("Complex data is not supported.");                
    } else {
        mexErrMsgTxt("samples should be a scalar."); 
    } 
    
    if ( isscalar( prhs[2] ) ){
        INT16 scalar;
        /* Returns the scalar, casts to int if necessary. Returns 0 if */
        /* input is complex. */
        if ( returnscalar( prhs[2], &e ) != 1 )
            mexErrMsgTxt("Complex data is not supported.");                
    } else {
        mexErrMsgTxt("events should be a scalar."); 
    } 
    
    plhs[0] = mxCreateNumericArray( 2, td, mxDOUBLE_CLASS, mxREAL);
    status = mxGetPr(plhs[0]);    
    *status = (double ) _main( (UINT32 ) m, (INT16 ) s, (INT16 ) e );  
    return;
}
